
package com.shop.cloud.mall.admin.controller;

import com.shop.cloud.common.core.util.R;
import com.shop.cloud.mall.admin.util.UserOnlineUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.time.Duration;

/**
 * 商城用户在线统计
 *
 * @author
 * @date 2022-06-04 11:09:55
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/useronline")
@Tag(description = "useronline", name = "商城用户在线统计管理")
public class UserOnlineController {

	private final UserOnlineUtil userOnlineUtil;

    /**
     * 获取指定商城端在线用户的数量
     * @return
     */
	@Operation(summary = "获取指定商城端在线用户的数量")
    @GetMapping("/count/{clientType}")
    public R getCountByClientType(@PathVariable("clientType") String clientType) {
		//获取5分钟内，发送过心跳包的用户数量，也就是在线用户的数量
        return R.ok(userOnlineUtil.count(clientType, Duration.ofMinutes(5)));
    }

	/**
	 * 获取指定商城端用户的最后在线时间
	 * @return
	 */
	@Operation(summary = "获取指定商城端用户的最后在线时间")
	@GetMapping("/score/{clientType}/{userId}")
	public R getScore(@PathVariable("clientType") String clientType, @PathVariable("userId") String userId) {
		return R.ok(userOnlineUtil.getScore(clientType, userId));
	}
}
